package com.qf.ssm.service.impl;
/**
 * @author 86155
 * @ClassName: com.qf.ssm.service.impl.CartAppServiceImpl
 * @Description: 该类的功能描述
 * @date: 2021/10/12 20:15
 */

import com.qf.ssm.common.exception.ServiceException;
import com.qf.ssm.common.request.CartRequestParams;
import com.qf.ssm.common.request.OrderRequestParams;
import com.qf.ssm.common.vo.ResponseCartMoney;
import com.qf.ssm.common.vo.ResponseCode;
import com.qf.ssm.entity.Cart;
import com.qf.ssm.entity.Order;
import com.qf.ssm.entity.Product;
import com.qf.ssm.mapper.CartAppMapper;
import com.qf.ssm.service.CartAppService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;


@Service
public class CartAppServiceImpl implements CartAppService {
    @Resource
    private CartAppMapper cartAppMapper;

    @Override
    public Boolean add(CartRequestParams cartRequestParams) {
        Integer count;
        try {
            /**
             * 判断用户是否已添加到购物车   用户ID、商品ID结合判断
             */
            Cart cart = cartAppMapper.selectCart(cartRequestParams.getMemberId(), cartRequestParams.getProductId());
            if (cart == null) {
                //添加
                cart = new Cart();
                BeanUtils.copyProperties(cartRequestParams, cart);
                count = cartAppMapper.insert(cart);
            } else {
                //  更新的操作   数量的计算  >1
                int quantity = cartRequestParams.getQuantity() > 1 ? cartRequestParams.getQuantity() : 1;
                cart.setQuantity(cart.getQuantity() + quantity);
                count = cartAppMapper.updateById(cart);
            }
        } catch (Exception ex) {
            throw new ServiceException(ResponseCode.SYS_ERROR);
        }
        return count > 0;
    }


    /**
     * 批量删除购物车商品，设置状态status=0
     *
     * @param ids
     * @param memberId
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Integer batchDel(List<Long> ids, Long memberId) {
        Integer delete = cartAppMapper.batch(ids, memberId);
        if (delete == ids.size()) {
            return delete;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 计算购物车总原价和优惠后总价格
     *
     * @param
     * @return
     */
    @Override
    public  ResponseCartMoney cartMoney(Long memberId) {
        ResponseCartMoney cartMoney = new ResponseCartMoney();
        double totalPrice = 0;
        double discount = 0;
        double discountTotalPrice = 0;

        List<Product> productList = cartAppMapper.selectProductList(memberId);

        if (productList == null || productList.size() == 0) {
            cartMoney.setTotalPrice(totalPrice);
            cartMoney.setDiscountTotalPrice(discountTotalPrice);
            return cartMoney;
        } else {
            try {
                for (int i = 0; i < productList.size(); i++) {
                    Product product = productList.get(i);
                    //计算原价 总价
                    double productPrice = product.getProductPrice().doubleValue();
                    totalPrice = totalPrice + productPrice;
                    //判断商品是否有折扣，discountId>0说明该商品有优惠
                    Long discountId = product.getDiscountId();
                    if (discountId != null && discountId > 0) {
                        //找到每个商品的优惠后的价格
                        BigDecimal discountPrice = cartAppMapper.selectDiscountPrice(product);
                        double discountPrice2 = productPrice - discountPrice.doubleValue();
                        discount = +discountPrice2;
                    }
                }
                cartMoney.setTotalPrice(totalPrice);
                cartMoney.setDiscountTotalPrice(totalPrice - discount);
                return cartMoney;
            } catch (ServiceException e) {
                throw new ServiceException(ResponseCode.ERROR);
            }
        }
    }

    /**
     * 先展示勾选的商品--计算要付款的金额--创建订单
     *
     * @param orderRequestParams
     * @return
     */
    @Override
    public Boolean creatOrder(OrderRequestParams orderRequestParams) {
        //返回了要创建的订单的信息
        Integer insert = null;
        try {
            Order order = new Order();
            BeanUtils.copyProperties(orderRequestParams, order);
            //设置订单状态，未付款
            order.setOrderName(1);
            //创建订单号
            order.setOrderNumber(Calendar.getInstance().getTimeInMillis());
            insert = cartAppMapper.insertOrder(order);
        } catch (ServiceException e) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return insert > 0;
    }
}
